package com.example.client2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;

import com.example.demo2.IHelloService;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView v = findViewById(R.id.client_view);
        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IHelloService helloService = IHelloService.Stub.asInterface(service);
                try {
                    String text = helloService.sayHello("morningcat");
                    v.setText(text);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {}
        };
        Intent intent = new Intent();
        intent.setAction("com.example.service.action");
        intent.setPackage("com.example.demo2");
        bindService(intent, connection, BIND_AUTO_CREATE);
    }
}